package com.joeyzh.utils;


import com.joey.base.util.DataFormatUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

import static com.joey.base.util.DataFormatUtils.getCurrentTimeInLong;

/**
 * Created by Joey on 2019/3/16.
 *
 * @author by Joey
 */

public class DateFormatConsts {
    public static final String FORMATTER_DATE_AND_TIME_TZ = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
    public static final String FORMATTER_DATE_AND_TIME_T = "yyyy-MM-dd'T'HH:mm:ss";
    public static final String FORMATTER_DATE_AND_TIME_HHSS = "yyyy-MM-dd HH:mm:ss.SSS";


    public static Date convertStrToDate(String var0, String var1) {
        SimpleDateFormat var2 = new SimpleDateFormat(var1);
        var2.setTimeZone(TimeZone.getTimeZone("UTC"));
        try {
            Date var3 = var2.parse(var0);
            return var3;
        } catch (ParseException var4) {
            var4.printStackTrace();
            return null;
        }
    }

    public static String convertDateToStr(String var0,Date date){
        SimpleDateFormat var2 = new SimpleDateFormat(var0);
        var2.setTimeZone(TimeZone.getTimeZone("UTC"));
        return var2.format(date);
    }


    /**
     * 无小时转换
     *
     * @param date
     * @return
     */
    public static String convertDateToStrNoTime(Date date, String var0) {
        date.setHours(0);
        date.setMinutes(0);
        date.setSeconds(0);
        return DataFormatUtils.convertDateToStr(date, var0);
    }


    /**
     * 无小时转换
     * @param date 时间
     * @param var0 格式
     * @param day  天数
     * @return
     */
    public static String convertDateToStrNoTime(Date date, String var0, int day) {
        date.setDate(date.getDate() + day);
        return DataFormatUtils.convertDateToStr(date, var0);
    }

    /**
     * 无小时转换 UTC
     *
     * @param date 时间
     * @param var0 格式
     * @param day  天数
     * @return
     */
    public static String convertDateToStrNoTimeTUC(Date date, String var0, int day) {
        date.setDate(date.getDate() + day);
        return convertDateToStrNoTimeTUC(date, var0);
    }

    /**
     * 无小时转换 UTC
     *
     * @param date
     * @return
     */
    public static String convertDateToStrNoTimeTUC(Date date, String var0) {
        date.setHours(0);
        date.setMinutes(0);
        date.setSeconds(0);
        return convertDateToStr(var0, date);
    }


    public static long convertDateStrToMillis(String var0, String format) {
        SimpleDateFormat var2 = new SimpleDateFormat(format);
        var2.setTimeZone(TimeZone.getTimeZone("UTC"));
        try {
            Date var3 = var2.parse(var0);
            return var3.getTime();
        } catch (ParseException var4) {
            var4.printStackTrace();
            return -1L;
        }
    }

    /**
     * 后台时间转本地时间
     *
     * @param var0   后台时间
     * @param format 时间格式
     * @return
     */
    public static String convertDateStrToStr(String var0, String format) {
        SimpleDateFormat var2 = new SimpleDateFormat(format);
        var2.setTimeZone(TimeZone.getTimeZone("UTC"));
        SimpleDateFormat var4 = new SimpleDateFormat(format);
        try {
            Date var3 = var2.parse(var0);
            return var4.format(var3);
        } catch (ParseException e) {
            e.printStackTrace();
            return "";
        }
    }

    /**
     * 本地时间转后台时间
     *
     * @param timeMills
     * @param format
     * @return
     */
    public static String getTime(long timeMills, String format) {
        SimpleDateFormat var3 = new SimpleDateFormat(format);
        var3.setTimeZone(TimeZone.getTimeZone("UTC"));
        return var3.format(new Date(timeMills));
    }

    /**
     * @param format 当前时间转后台时间
     * @return
     */
    public static String getCurrentTimeInString(String format) {
        return getTime(getCurrentTimeInLong(), format);
    }


    public static String getHHmmss(int period) {
        if (period < 0) {
            return "";
        }
        int hour = period / 60 / 60;
        int minue = (period - (hour * 60 * 60)) / 60;
        int second = (period - hour * 60 * 60 - minue * 60);
        if (hour == 0) {
            return String.format("%d Min", minue);
        }
        if (minue == 0) {
            return String.format("%d Hour", hour);
        }
        return String.format("%d Hour %d Min", hour, minue);
    }

    public static Date formatUADate(String original) {
        if (original == null || original.length() < FORMATTER_DATE_AND_TIME_T.length()) {
            return new Date();
        }
        String timeString = original.substring(0, DateFormatConsts.FORMATTER_DATE_AND_TIME_T.length() - 1);
        return DateFormatConsts.convertStrToDate(timeString, DateFormatConsts.FORMATTER_DATE_AND_TIME_T);
    }

    public static long formatUAMills(String original) {
        if (original == null || original.length() < FORMATTER_DATE_AND_TIME_T.length()) {
            return 0;
        }
        String timeString = original.substring(0, DateFormatConsts.FORMATTER_DATE_AND_TIME_T.length() - 1);
        return DateFormatConsts.convertDateStrToMillis(timeString, DateFormatConsts.FORMATTER_DATE_AND_TIME_T);
    }

}
